package com.ruoyi.blog.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.blog.domain.AcBlog;
import com.ruoyi.blog.service.IAcBlogService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.blog.domain.AcType;
import com.ruoyi.blog.service.IAcTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分类管理Controller
 * 
 * @author ruoyi
 * @date 2022-06-19
 */
@RestController
@RequestMapping("/blog/type")
public class AcTypeController extends BaseController
{
    @Autowired
    private IAcTypeService acTypeService;

    @Autowired
    private IAcBlogService acBlogService;

    @GetMapping("/getTypeList")
    public AjaxResult getTypeList()
    {
        Long userId = getUserId();
        AcType acType = new AcType();
        List<AcType> list = new ArrayList<>();
        for (AcType type : acTypeService.selectAcTypeList(acType)) {
            AcBlog blog = new AcBlog();
            blog.setUserId(getUserId());
            blog.setTypeId(type.getId());
            blog.setStatus(1);
            List<AcBlog> blogList = acBlogService.selectAcBlogList(blog);
            if (blogList.size() > 0) {
                type.setCount(blogList.size());
                list.add(type);
            }
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/getTypeBlog")
    public AjaxResult getTypeBlog(Long typeId)
    {
        AcBlog blog = new AcBlog();
        blog.setUserId(getUserId());
        blog.setTypeId(typeId);
        blog.setStatus(1);
        List<AcBlog> list = acBlogService.selectAcBlogList(blog);
        AcType type = acTypeService.selectAcTypeById(typeId);
        Collections.sort(list, (o1, o2) -> {
            if (o1.getIsTop() != o2.getIsTop()) return o2.getIsTop() - o1.getIsTop();
            return o2.getCreateTime().compareTo(o1.getCreateTime());
        });
        return AjaxResult.success(list).put("type", type.getName());
    }

    /**
     * 查询分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcType acType)
    {
        startPage();
        List<AcType> list = acTypeService.selectAcTypeList(acType);
        return getDataTable(list);
    }

    /**
     * 导出分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:type:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcType acType)
    {
        List<AcType> list = acTypeService.selectAcTypeList(acType);
        ExcelUtil<AcType> util = new ExcelUtil<AcType>(AcType.class);
        util.exportExcel(response, list, "分类管理数据");
    }

    /**
     * 获取分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(acTypeService.selectAcTypeById(id));
    }

    /**
     * 新增分类管理
     */
    @PreAuthorize("@ss.hasPermi('blog:type:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcType acType)
    {
        return toAjax(acTypeService.insertAcType(acType));
    }

    /**
     * 修改分类管理
     */
    @PreAuthorize("@ss.hasPermi('blog:type:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcType acType)
    {
        return toAjax(acTypeService.updateAcType(acType));
    }

    /**
     * 删除分类管理
     */
    @PreAuthorize("@ss.hasPermi('blog:type:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(acTypeService.deleteAcTypeByIds(ids));
    }
}
